import throttle from '../popup/utils/throttle';
// import { ElNotification } from 'element-plus';

chrome.storage.local.get({ autoLikes: false }, (result) => {
  result.autoLikes ? likesHandel() : '';
});
// chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
//   console.log('sender' + sender );
//   if (request.cmd == 'test') alert(request.value);
//   sendResponse('我收到了你的消息！');
// });

chrome.storage.onChanged.addListener(function (changes, namespace) {
  for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
    // key存储的名字 namespace存储的类型local或者sync oldValue存储更新前的数据,首次存储为undefined newValue存储更新后的数据
    console.log(key, namespace, oldValue, newValue);
    switch (key) {
      case 'autoLikes':
        likesHandel();
        break;
      default:
        break;
    }
  }
});

const likesHandel = function () {
  console.log('likesHandel');
  const el = document.querySelector('.like');
  // 如果已经点赞
  if (el?.classList?.includes('on')) return;
  // 获取播放的video标签
  const videoEl = document.querySelector('bwp-video');
  console.log(videoEl);
  const duration = videoEl?.duration;
  if (!videoEl || !videoEl.src || !duration) return;
  videoEl.addEventListener('playing', () => {
    throttle(() => {
      const percentage = Math.round(videoEl.currentTime / videoEl.duration);
      console.log('播放进度:' + parseFloat(videoEl.currentTime));
      console.log('百分比进度:' + percentage * 100);
      if (percentage >= 1) {
        el.click();
        // ElNotification({
        //   title: 'Success',
        //   message: '我谁啊我',
        //   type: 'success',
        // });
      }
    }, 1000);
  });
};
export { likesHandel };
